
/*
 * 文件名：c_utils.c
 * 功能描述：C语言工具函数库
 */
#include "c_utils.h"
#include <Arduino.h>




///______________________________________________________________________________________________

uint8_t GetHigh4bit(uint8_t data) {
	uint8_t height;
	height = ( ( data & 0xf0 ) >> 4 );
	return height;
}

uint8_t GetLow4bit(uint8_t data) {
	uint8_t low;
	low = ( data & 0x0f );
	return low;
}

uint8_t CalcSum(uint8_t *arr, uint16_t len) {
	uint8_t sum = 0;
	for (int i = 0; i < len; ++i) {
		sum += arr[i];
	}
	return (uint8_t) sum;
}

/**
 * @brief	�жϴ�С��
 * @param
 * @retval
 */
bool Bsp_IsLittleEndian(void){
	uint16_t i = 1;
	if ( *((uint8_t *)(&i)) == 1){
		return true;
	}else{
		return false;
	}
}


/* ͨ���ƥ��, ƥ��֧��:
 * ?:һ��
 * *: [0-N]
 * Example:
 * WildcardMatch("Go*", "Go1234") = 1
 * WildcardMatch("Go*", "Go") = 1
 * WildcardMatch("Go?", "Go-") = 1
 * WildcardMatch("Go?", "Go") = 0
 * WildcardMatch("*Go*T", "Go1234T") = 1
 * WildcardMatch("*Go??", "Go12") = 1
 * WildcardMatch("*Go???", "Go123") = 1
 * WildcardMatch("*Go????T", "Go1234T") = 1
 * WildcardMatch("*Go????T", "Go123T") = 0
 */
bool WildcardMatch(const char *sPat, const char *sStr) {
	const char * s;
	const char * p;
	int          Star;
	//
	Star = 0;
	//
	Again:
	for (s = sStr, p = sPat; *s; ++s, ++p) {
		switch (*p) {
			case '?':
				break;
				//
			case '*':
				Star = 1;
				sStr = s;
				sPat = p;
				if (*++sPat == 0) {
					return true;
				}
				goto Again;
				//
			default:
				if (*s != *p) {
					if (!Star) {
						return false;
					}
					++sStr;
					goto Again;
				}
				break;
		}
	}
	if (*p == '*') {
		++p;
	}
	return *p == 0;
}


/* ����x^y
 * 	for(int i=0;i<10;++i) {
		printf("%ld\r\n",x_power_y(10,i));
	}
	1
	10
	100
	1000
	10000
	100000
	1000000
	10000000
	100000000
	1000000000
 */
uint32_t x_power_y(uint32_t x, uint8_t y) {
	uint32_t output = 1;

	/* Make a "power" multiply */
	while (y--) {
		output *= x;
	}

	/* Return output value */
	return output;
}

/* ���ر����������2������ */
uint32_t NextPowerOf2(uint32_t number) {
	/* Check number */
	if (number <= 1) {
		return 1;
	}

	/* Do some bit operations */
	number--;
	number |= number >> 1;
	number |= number >> 2;
	number |= number >> 4;
	number |= number >> 8;
	number |= number >> 16;
	number++;

	/* Return calculated number */
	return number;
}



int split(char *src,const char *separator,char **dest) {
	int split_cnt = 0;
	char *pNext;

	if (src == NULL || strlen(src) == 0)
		return 0;
	if (separator == NULL || strlen(separator) == 0)
		return 0;
	pNext = strtok(src,separator);
	while(pNext != NULL) {
		*dest++ = pNext;
		++split_cnt;
		pNext = strtok(NULL,separator);
	}

	return split_cnt;
}


typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
	while (BufferLength--)
	{
		if (*pBuffer1 != *pBuffer2)
		{
			return FAILED;
		}

		pBuffer1++;
		pBuffer2++;
	}

	return PASSED;
}



/*______________________________END___________________________________________*/

